package com.vinci.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 自定义拦截器，用来处理用户登入问题
 */

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //提取user来验证登入
        HttpSession session= request.getSession();
        //这是登入时候放入session的
        Object loginuser = session.getAttribute("loginuser");
        //如果session没有user 表示没有登录
        if(loginuser==null||loginuser.equals("")){
            request.setAttribute("msg", "无权限请先登录");
            request.getRequestDispatcher("/index.html").forward(request, response);
            return false;


        }
         //如果session中有loginuser直接放行即可
        return true;


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
